﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Text;
using System.Web.Http;

namespace 穷举.Controllers
{
    /// <summary>
    /// 基本的请求实例
    /// </summary>
    public class BasicRequestController : ApiController
    {

        #region Get

        /// <summary>
        /// 获取一个字符串
        /// </summary>
        /// <returns>返回值：字符串</returns>
        [HttpGet]
        public string GetString()
        {
            return "OKOKOKOKOK";
        }

        /// <summary>
        /// 获取员工信息
        /// </summary>
        /// <param name="id">员工Id</param>
        /// <returns>返回值：字符串</returns>
        [HttpGet]
        public string GetString(int id)
        {
            return "{\"empID\":\"8E3844B8-81CF-42A7-99CF-5EB29E345AEF\",\"empNO\":\"113342\",\"empName\":\"邓大军\",\"empTel\":\"13824320449\",\"email\":\"dengdj2@centaline.com.cn\",\"flagDeleted\":0,\"sex\":\"男\",\"domainAccount\":\"dengdj2\",\"empStatus\":\"在职\",\"empModDate\":\"2019-09-04 09:25:58\",\"modDate\":\"2019-09-04 09:25:58\",\"idCard\":\"511724200001280371\",\"mD5IDCard18\":\"/x+qZEj8Voop2eMm7nvs1Q==\",\"inGroupDate\":\"2019-05-20 00:00:00\",\"inCompanyDate\":\"2019-05-20 00:00:00\",\"dimissionDate\":null,\"dateBirth\":\"2000-01-28\",\"education\":\"中等专科\",\"nativePlace\":\"四川省大竹县\",\"homeAddress\":\"四川省大竹县双拱镇双拱村5组\",\"jobLevel\":\"1\",\"jobGrade\":\"试用\",\"positionID\":\"a7f9a072-a2a5-48a7-afb6-82dd236b1df2\",\"positionName\":\"物业顾问\",\"empPositionModDate\":\"2019-09-04 09:25:58\",\"deptID\":\"f35453b3-65d2-4fce-8536-7669090418f9\",\"deptName\":\"冠城世家店二组\",\"deptPath\":\"KD01.0001.0043.0008.0005.0002.0002\",\"hroc\":\"044S717\",\"flagPrincipal\":0,\"empType\":\"试用员工\",\"inDueDate\":\"2019-08-20 00:00:00\",\"flagDeptKind\":3}";
        }

        /// <summary>
        /// 获取一个Json类型的返回值
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetJSON(int id, string name)
        {
            UserInfo user = new UserInfo()
            {
                Id = id,
                Name = name,
                Address = "Address111",
                YouInput = "Id = " + id + ", name = " + name
            };
            string json = JsonConvert.SerializeObject(user);

            //返回json数
            return new HttpResponseMessage()
            {
                Content = new StringContent(json, Encoding.UTF8, "application/json"),
            };
        }

        /// <summary>
        /// 获取一个文件流
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>文件流</returns>
        [HttpGet]
        public HttpResponseMessage GetImage(int id)
        {
            string tempStr = Get() + "Images\\gamersky_03origin_05_2018911618340.jpg";

            //输出到浏览器
            try
            {
                var stream = new FileStream(tempStr, FileMode.Open);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(stream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "gamersky_03origin_05_2018911618340.jpg"
                };
                return response;
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.NoContent);
            }
        }
        #endregion


        #region Post

        #region 基本的Post提交
        [HttpPost]
        public string PostString()
        {
            return "OK";
        }

        /// <summary>
        /// 获取员工信息
        /// </summary>
        /// <param name="id">员工Id</param>
        /// <returns>返回值：字符串</returns>
        [HttpPost]
        public string PostString(int id)
        {
            return "{\"empID\":\"8E3844B8-81CF-42A7-99CF-5EB29E345AEF\",\"empNO\":\"113342\",\"empName\":\"邓大军\",\"empTel\":\"13824320449\",\"email\":\"dengdj2@centaline.com.cn\",\"flagDeleted\":0,\"sex\":\"男\",\"domainAccount\":\"dengdj2\",\"empStatus\":\"在职\",\"empModDate\":\"2019-09-04 09:25:58\",\"modDate\":\"2019-09-04 09:25:58\",\"idCard\":\"511724200001280371\",\"mD5IDCard18\":\"/x+qZEj8Voop2eMm7nvs1Q==\",\"inGroupDate\":\"2019-05-20 00:00:00\",\"inCompanyDate\":\"2019-05-20 00:00:00\",\"dimissionDate\":null,\"dateBirth\":\"2000-01-28\",\"education\":\"中等专科\",\"nativePlace\":\"四川省大竹县\",\"homeAddress\":\"四川省大竹县双拱镇双拱村5组\",\"jobLevel\":\"1\",\"jobGrade\":\"试用\",\"positionID\":\"a7f9a072-a2a5-48a7-afb6-82dd236b1df2\",\"positionName\":\"物业顾问\",\"empPositionModDate\":\"2019-09-04 09:25:58\",\"deptID\":\"f35453b3-65d2-4fce-8536-7669090418f9\",\"deptName\":\"冠城世家店二组\",\"deptPath\":\"KD01.0001.0043.0008.0005.0002.0002\",\"hroc\":\"044S717\",\"flagPrincipal\":0,\"empType\":\"试用员工\",\"inDueDate\":\"2019-08-20 00:00:00\",\"flagDeptKind\":3}";
        }

        /// <summary>
        /// 获取一个Json类型的返回值
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage PostJSON(int id, string name)
        {
            UserInfo user = new UserInfo()
            {
                Id = id,
                Name = name,
                Address = "Address"
            };
            string json = JsonConvert.SerializeObject(user);

            //返回json数
            return new HttpResponseMessage()
            {
                Content = new StringContent(json, Encoding.UTF8, "application/json"),
            };
        }
        #endregion

        #region form-data Post 提交
        [HttpPost]
        public HttpResponseMessage PostFormData1()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象   
            string id = request.Form["id"];
            string name = request.Form["name"];
            string json = JsonConvert.SerializeObject(new
            {
                msg = "Form 表单中提交的参数Id，和 Name 的值",
                id = id,
                name = name
            });
            //返回json数
            return new HttpResponseMessage()
            {
                Content = new StringContent(json, Encoding.UTF8, "application/json"),
            };
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string PostImage()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象   
            string filePath = Get();
            string file = "";
            foreach (string item in request.Files)
            {
                HttpPostedFileBase postFile = request.Files[item];
                if (postFile.ContentLength == 0)
                {
                    continue;
                }

                string newFilePath = "/Images";
                file = newFilePath + "/" + string.Format("{0:hhmmss}", DateTime.Now) + "_" + System.Guid.NewGuid() + "." + postFile.FileName.Split('.').Last();
                filePath += file;

                if (postFile.ContentLength == 0)
                {
                    continue;
                }
                postFile.SaveAs(filePath);

            }
            return file;
        }
        #endregion

        #region PostJSON类型请求参数
        [HttpPost]
        public HttpResponseMessage PostRequestJson()
        {
            string json = Request.Content.ReadAsStringAsync().Result;
            return new HttpResponseMessage()
            {
                Content = new StringContent(json, Encoding.UTF8, "application/json"),
            };
        }
        #endregion

        #endregion


        #region 私有方法

        /// <summary>
        /// 获取当前项目的根目录
        /// </summary>
        /// <returns></returns>
        private string Get()
        {
            return System.Web.Hosting.HostingEnvironment.MapPath("/");
        }
        #endregion


    }

    public class UserInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string YouInput { get; set; }
    }
}
